.PHONY:all sim clean

OBJ_DIR = obj_dir

CFILE = .cpp
HFILE = .h
VFILE = .sv
WAVEFILE = wave.vcd

BINSRC ?=
LOGSRC = ${NPC_HOME}/npc-log.txt
ELFSRC ?=
SOSRC  ?=


VSRC += $(shell find $(abspath ./vsrc) -name "*.sv")
VSRC += $(shell find ~/ysyxSoC/perip -name "*.v")
VSRC += $(shell find ~/ysyxSoC/perip -name "*.sv")
VSRC += $(shell find ~/ysyxSoC/build -name "*.v")
VINC += -I$(abspath ./vsrc)
VINC += -I$(abspath ./vsrc/AxiFullAdapter)
# VINC += -I$(abspath ./vsrc/Cache)
VINC += -I/home/finalx/ysyxSoC/perip/uart16550/rtl
VINC += -I/home/finalx/ysyxSoC/perip/spi/rtl
# VINC += -I/home/finalx/ysyxSoC/perip/psram
VINC += -I/home/finalx/ysyxSoC/perip/sdram
CSRC = $(shell find $(abspath ./csrc) -name "*.cpp")
CINC += -I$(abspath ./cinc)


# VTOP = RISCV_BOARD
VTOP = ysyxSoCFull
TGS = V${VTOP}

# START - 链接llvm
CXXSRC = csrc/trace/disasm.cpp
CXXFLAGS += $(shell llvm-config --cxxflags) -fPIE
LIBS += -lreadline
LIBS += $(shell llvm-config --libs)
LIBS += -ldl
LIBS += -lSDL2

$(OBJ_DIR)/%.o: %.cc
	@echo + CXX $<
	@mkdir -p $(dir $@)
	@$(CXX) $(CFLAGS) $(CXXFLAGS) -c -o $@ $<
	#$(call call_fixdep, $(@:.o=.d), $@)
# END



sim-all:
	@echo ${VSRC}


sim:
	# $(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
	verilator --cc --exe -top-module ${VTOP} --build --trace -trace-structs --autoflush \
	--timescale "1ns/1ns" --no-timing \
	${VINC} \
	-LDFLAGS "${LIBS} -MMD" \
	-CFLAGS "${CINC} -g -w" \
	${CSRC} ${VSRC}
	make -C obj_dir -f ${TGS}.mk ${TGS}
	./obj_dir/${TGS} ${BINSRC} ${LOGSRC} ${ELFSRC} ${SOSRC}

sim_trace:
	# $(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
	verilator -Wall --cc --exe -top-module ${VTOP} --build --trace -trace_structs \
	${VINC} \
	-LDFLAGS "${LIBS} -MMD" \
	-CFLAGS "${CINC} -g -w" \
	${CSRC} ${VSRC}
	make -C obj_dir -f ${TGS}.mk ${TGS}
	./obj_dir/${TGS} ${BINSRC} ${LOGSRC} ${ELFSRC} ${SOSRC}


gdb:
	# $(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
	verilator --cc --exe -top-module ${VTOP} --build --trace -trace-structs \
	--timescale "1ns/1ns" --no-timing \
	${VINC} \
	-LDFLAGS "${LIBS} -MMD" \
	-CFLAGS "${CINC} -g -w" \
	${CSRC} ${VSRC}
	make -C obj_dir -f ${TGS}.mk ${TGS}
	gdb --args ./obj_dir/${TGS} ${BINSRC} ${LOGSRC} ${ELFSRC} ${SOSRC}


sim-clean:
	@echo "remove <NPC>/obj_dir:"
	rm obj_dir -rf

include ../Makefile
